* This is the do file to create "Table C4. Heterogeneity across Baseline Abilites (Matched Sample, Math and CPCS)" and "Table C5. Heterogeneity across Baseline Abilites (Matched Sample, Math and RSES)"
set seed 123

use "$path_data/temp/followup_student_parents_matched", clear

gen gend = q1d - 1

local controls DT_score_pre_std DT_time_pre rosen_pre_std cpcs_pre_std i.grade gend branch1 branch2 branch3 income_source1 income_source2 income_source3 income_source4 last_income_per_member hhmember hhheadage hhheadeduyear phone_survey age_tchr
teffects psmatch (followup_cog_std) (treatment `controls') 
psmatch2 treatment `controls', outcome(followup_cog_std) noreplacement
gen psmattrition = 1 if _support!=1
recode psmattrition (.=0)
keep if psmattrition == 0

egen DT_score_pre_std_med = median(DT_score_pre_std)
gen DT_score_pre_std_upper50 = 1 if DT_score_pre_std>DT_score_pre_std_med
recode DT_score_pre_std_upper50 (.=0)

egen rosen_pre_std_med = median(rosen_pre_std)
gen rosen_pre_std_upper50 = 1 if rosen_pre_std>rosen_pre_std_med
recode rosen_pre_std_upper50 (.=0)
rename rosen_pre_std_upper50 RSES_std_upper50

egen cpcs_pre_std_med = median(cpcs_pre_std)
gen cpcs_pre_std_upper50 = 1 if cpcs_pre_std>cpcs_pre_std_med
recode cpcs_pre_std_upper50 (.=0)
rename cpcs_pre_std_upper50 CPCS_std_upper50


/// Cognitive
wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 1 & RSES_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_cog_u_rses_u = e(N)
scalar n_clust_cog_u_rses_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogU_rsesU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogU_rsesU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogU_rsesU_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogU_rsesU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 1 & RSES_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_cog_u_rses_l = e(N)
scalar n_clust_cog_u_rses_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogU_rsesL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogU_rsesL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogU_rsesL_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogU_rsesL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 0 & RSES_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_cog_l_rses_u = e(N)
scalar n_clust_cog_l_rses_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogL_rsesU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogL_rsesU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogL_rsesU_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogL_rsesU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 0 & RSES_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_cog_l_rses_l = e(N)
scalar n_clust_cog_l_rses_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogL_rsesL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogL_rsesL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogL_rsesL_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogL_rsesL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 1 & CPCS_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_cog_u_cpcs_u = e(N)
scalar n_clust_cog_u_cpcs_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogU_cpcsU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogU_cpcsU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogU_cpcsU_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogU_cpcsU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 1 & CPCS_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_cog_u_cpcs_l = e(N)
scalar n_clust_cog_u_cpcs_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogU_cpcsL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogU_cpcsL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogU_cpcsL_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogU_cpcsL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 0 & CPCS_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_cog_l_cpcs_u = e(N)
scalar n_clust_cog_l_cpcs_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogL_cpcsU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogL_cpcsU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogL_cpcsU_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogL_cpcsU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 0 & CPCS_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_cog_l_cpcs_l = e(N)
scalar n_clust_cog_l_cpcs_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cog_cogL_cpcsL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cog_cogL_cpcsL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cog_cogL_cpcsL_se[1,`j'] = temp[2,`j']
* p value
matrix cog_cogL_cpcsL_pv[1,`j'] = temp[3,`j']
}




/// RSES
wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 1 & RSES_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_rses_u_cog_u = e(N)
scalar n_clust_rses_u_cog_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogU_rsesU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogU_rsesU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogU_rsesU_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogU_rsesU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 1 & RSES_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_rses_l_cog_u = e(N)
scalar n_clust_rses_l_cog_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogU_rsesL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogU_rsesL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogU_rsesL_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogU_rsesL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 0 & RSES_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_rses_u_cog_l = e(N)
scalar n_clust_rses_u_cog_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogL_rsesU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogL_rsesU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogL_rsesU_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogL_rsesU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 0 & RSES_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_rses_l_cog_l = e(N)
scalar n_clust_rses_l_cog_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogL_rsesL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogL_rsesL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogL_rsesL_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogL_rsesL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 1 & CPCS_std_upper50 == 1, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogU_cpcsU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogU_cpcsU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogU_cpcsU_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogU_cpcsU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 1 & CPCS_std_upper50 == 0, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogU_cpcsL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogU_cpcsL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogU_cpcsL_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogU_cpcsL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 0 & CPCS_std_upper50 == 1, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogL_cpcsU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogL_cpcsU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogL_cpcsU_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogL_cpcsU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg RSES_std treatment if DT_score_pre_std_upper50 == 0 & CPCS_std_upper50 == 0, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix rses_cogL_cpcsL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix rses_cogL_cpcsL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix rses_cogL_cpcsL_se[1,`j'] = temp[2,`j']
* p value
matrix rses_cogL_cpcsL_pv[1,`j'] = temp[3,`j']
}




/// CPCS

wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 1 & RSES_std_upper50 == 1, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogU_rsesU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogU_rsesU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogU_rsesU_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogU_rsesU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 1 & RSES_std_upper50 == 0, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogU_rsesL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogU_rsesL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogU_rsesL_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogU_rsesL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 0 & RSES_std_upper50 == 1, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogL_rsesU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogL_rsesU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogL_rsesU_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogL_rsesU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 0 & RSES_std_upper50 == 0, cluster(school_no) reps(1000)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogL_rsesL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogL_rsesL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogL_rsesL_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogL_rsesL_pv[1,`j'] = temp[3,`j']
}


wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 1 & CPCS_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_cpcs_u_cog_u = e(N)
scalar n_clust_cpcs_u_cog_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogU_cpcsU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogU_cpcsU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogU_cpcsU_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogU_cpcsU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 1 & CPCS_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_cpcs_l_cog_u = e(N)
scalar n_clust_cpcs_l_cog_u = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogU_cpcsL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogU_cpcsL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogU_cpcsL_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogU_cpcsL_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 0 & CPCS_std_upper50 == 1, cluster(school_no) reps(1000)
scalar n_cpcs_u_cog_l = e(N)
scalar n_clust_cpcs_u_cog_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogL_cpcsU_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogL_cpcsU_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogL_cpcsU_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogL_cpcsU_pv[1,`j'] = temp[3,`j']
}

wildbootstrap reg CPCS_std treatment if DT_score_pre_std_upper50 == 0 & CPCS_std_upper50 == 0, cluster(school_no) reps(1000)
scalar n_cpcs_l_cog_l = e(N)
scalar n_clust_cpcs_l_cog_l = e(N_clust)
matrix temp = r(table)
foreach s in mean se pv obs {
		matrix cpcs_cogL_cpcsL_`s' = J(1,2,.)
}
foreach j in 1 2 {
* beta
matrix cpcs_cogL_cpcsL_mean[1,`j'] = temp[1,`j']
* standard error
* matrix cpcs_cogL_cpcsL_se[1,`j'] = temp[2,`j']
* p value
matrix cpcs_cogL_cpcsL_pv[1,`j'] = temp[3,`j']
}

// significant level


local outcome cog rses cpcs
local hetero1 cogU cogL
local hetero2 rsesU rsesL

foreach dep in `outcome'{
foreach h1 in `hetero1'{
foreach h2 in `hetero2'{
		if `dep'_`h1'_`h2'_pv[1,1]<=0.01 {
			local star_`dep'_`h1'_`h2' %3s "***"
		}
		else if (`dep'_`h1'_`h2'_pv[1,1]>0.01) & (`dep'_`h1'_`h2'_pv[1,1]<=0.05) {
			local star_`dep'_`h1'_`h2' %2s "**"
		}
		else if (`dep'_`h1'_`h2'_pv[1,1]>0.05) & (`dep'_`h1'_`h2'_pv[1,1]<=0.10) {
			local star_`dep'_`h1'_`h2' %1s "*"
		}
		else {
			local star_`dep'_`h1'_`h2'  ""
		}
} 
}
}

local outcome cog rses cpcs
local hetero1 cogU cogL
local hetero2 cpcsU cpcsL

foreach dep in `outcome'{
foreach h1 in `hetero1'{
foreach h2 in `hetero2'{
		if `dep'_`h1'_`h2'_pv[1,1]<=0.01 {
			local star_`dep'_`h1'_`h2' %3s "***"
		}
		else if (`dep'_`h1'_`h2'_pv[1,1]>0.01) & (`dep'_`h1'_`h2'_pv[1,1]<=0.05) {
			local star_`dep'_`h1'_`h2' %2s "**"
		}
		else if (`dep'_`h1'_`h2'_pv[1,1]>0.05) & (`dep'_`h1'_`h2'_pv[1,1]<=0.10) {
			local star_`dep'_`h1'_`h2' %1s "*"
		}
		else {
			local star_`dep'_`h1'_`h2'  ""
		}
} 
}
}

/// Table



tempname hh2
file open `hh2' using "$path_output/hetero_2by2_RSES_matchsample.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Heterogeneity among Baseline Abilites (Matched Sample, Math and RSES)}" _newline
file write `hh2' "\label{tab:hetero_rses_match}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccccccc}\toprule" _newline

 

  
file write `hh2' " Dependent Variable & \multicolumn{2}{c}{Baseline^{b}} & Difference & Obs & N of clusters \\\midrule\midrule" _newline
file write `hh2' " Rapid math test score^{a} & Math Top 50\%  & RSES Top 50\% & " %04.3f (cog_cogU_rsesU_mean[1,1]) `star_cog_cogU_rsesU' " & " %02.0f ( n_cog_u_rses_u ) " & " %02.0f ( n_clust_cog_u_rses_u ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogU_rsesU_pv[1,1]) " ) & &  \\ " _newline
file write `hh2' "  & & RSES Bottom 50\% & " %04.3f (cog_cogU_rsesL_mean[1,1]) `star_cog_cogU_rsesL' " & " %02.0f ( n_cog_u_rses_l ) " & " %02.0f ( n_clust_cog_u_rses_l ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogU_rsesL_pv[1,1]) " ) & &  \\ " _newline
file write `hh2' "  & Math Bottom 50\%  & RSES Top 50\% & " %04.3f (cog_cogL_rsesU_mean[1,1]) `star_cog_cogL_rsesU' " & " %02.0f ( n_cog_l_rses_u ) " & " %02.0f ( n_clust_cog_l_rses_u ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogL_rsesU_pv[1,1]) " ) & &  \\ " _newline
file write `hh2' "  & & RSES Bottom 50\% & " %04.3f (cog_cogL_rsesL_mean[1,1]) `star_cog_cogL_rsesL' " & " %02.0f ( n_cog_l_rses_l ) " & " %02.0f ( n_clust_cog_l_rses_l ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogL_rsesL_pv[1,1]) " )  & &  \\ " _newline

file write `hh2' " RSES score^{a} & Math Top 50\%  & RSES Top 50\% & " %04.3f (rses_cogU_rsesU_mean[1,1]) `star_rses_cogU_rsesU' " & " %02.0f ( n_rses_u_cog_u ) " & " %02.0f ( n_clust_rses_u_cog_u ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (rses_cogU_rsesU_pv[1,1]) " ) & &  \\ " _newline
file write `hh2' "  & & RSES Bottom 50\% & " %04.3f (rses_cogU_rsesL_mean[1,1]) `star_rses_cogU_rsesL' " & " %02.0f ( n_rses_l_cog_u ) " & " %02.0f ( n_clust_rses_l_cog_u ) " \\ " _newline
file write `hh2' "    			&	& & ( " %04.3f (rses_cogU_rsesL_pv[1,1]) " ) & &  \\ " _newline
file write `hh2' "  & Math Bottom 50\%  & RSES Top 50\% & " %04.3f (rses_cogL_rsesU_mean[1,1]) `star_rses_cogL_rsesU' " & " %02.0f ( n_rses_u_cog_l ) " & " %02.0f ( n_clust_rses_u_cog_l ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (rses_cogL_rsesU_pv[1,1]) " ) & &  \\ " _newline
file write `hh2' "  & & RSES Bottom 50\% & " %04.3f (rses_cogL_rsesL_mean[1,1]) `star_rses_cogL_rsesL' " & " %02.0f ( n_rses_l_cog_l ) " & " %02.0f ( n_clust_rses_l_cog_l ) " \\ " _newline
file write `hh2' "    			&	& & ( " %04.3f (rses_cogL_rsesL_pv[1,1]) " ) & &  \\ " _newline

file write `hh2' "\\\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Dependent variables are standardized using the average and variance of the whole follow-up sample in the February 2022 survey. " _newline
file write `hh2' "\item (b) For the propensity score matching, we use covariates including student's grade, sex, baseline cognitive and baseline non-cognitive score, DT baseline time, branch dummy (location), parents' income source, last income per family member, number of household members, age of household head, education level of household head, teacher's age, sex, and phone survey dummy." _newline
file write `hh2' "\item (c) Cutoffs are created based on whether their ability to perform each item at the baseline was higher or lower than the median. " _newline
file write `hh2' "\item (d) Wild cluster bootstrap p-values are reported within parentheses. Clusters are schools at the baseline." _newline
file write `hh2' "\item (e) $^*$ Significant at 10\% level; $^{**}$ significant at 5\% level; $^{***}$ significant at 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'


tempname hh2
file open `hh2' using "$path_output/hetero_2by2_CPCS_matchsample.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Heterogeneity among Baseline Abilites (Matched Sample, Math and CPCS)}" _newline
file write `hh2' "\label{tab:hetero_cpcs_match}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccccccc}\toprule" _newline

file write `hh2' " Dependent Variable & \multicolumn{2}{c}{Baseline^{b}} & Difference & Obs & N of clusters  \\\midrule\midrule" _newline
file write `hh2' " Rapid math test score^{a} & Math Top 50\%  & CPCS Top 50\% & " %04.3f (cog_cogU_rsesU_mean[1,1]) `star_cog_cogU_cpcsU' " & " %02.0f ( n_cog_u_cpcs_u ) " & " %02.0f ( n_clust_cog_u_cpcs_u ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogU_cpcsU_pv[1,1]) " )  & &   \\ " _newline
file write `hh2' "  & & CPCS Bottom 50\% & " %04.3f (cog_cogU_cpcsL_mean[1,1]) `star_cog_cogU_cpcsL' " & " %02.0f ( n_cog_u_cpcs_l ) " & " %02.0f ( n_clust_cog_u_cpcs_l ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogU_cpcsL_pv[1,1]) " )  & &  \\ " _newline
file write `hh2' "  & Math Bottom 50\%  & CPCS Top 50\% & " %04.3f (cog_cogL_cpcsU_mean[1,1]) `star_cog_cogL_cpcsU' " & " %02.0f ( n_cog_l_cpcs_u ) " & " %02.0f ( n_clust_cog_l_cpcs_u ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogL_cpcsU_pv[1,1]) " )  & &  \\ " _newline
file write `hh2' "  & & CPCS Bottom 50\% & " %04.3f (cog_cogL_cpcsL_mean[1,1]) `star_cog_cogL_cpcsL' " & " %02.0f ( n_cog_l_cpcs_l ) " & " %02.0f ( n_clust_cog_l_cpcs_l ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cog_cogL_cpcsL_pv[1,1]) " )  & &  \\ " _newline

file write `hh2' " CPCS score^{a} & Math Top 50\%  & CPCS Top 50\% & " %04.3f (cpcs_cogU_cpcsU_mean[1,1]) `star_cpcs_cogU_cpcsU' " & " %02.0f ( n_cpcs_u_cog_u ) " & " %02.0f ( n_clust_cpcs_u_cog_u ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cpcs_cogU_cpcsU_pv[1,1]) " ) & &   \\ " _newline
file write `hh2' "  & & CPCS Bottom 50\% & " %04.3f (cpcs_cogU_cpcsL_mean[1,1]) `star_cpcs_cogU_cpcsL' " & " %02.0f ( n_cpcs_l_cog_u ) " & " %02.0f ( n_clust_cpcs_l_cog_u ) " \\ " _newline
file write `hh2' "    			&	& & ( " %04.3f (cpcs_cogU_cpcsL_pv[1,1]) " )  & &  \\ " _newline
file write `hh2' "  & Math Bottom 50\%  & CPCS Top 50\% & " %04.3f (cpcs_cogL_cpcsU_mean[1,1]) `star_cpcs_cogL_cpcsU' " & " %02.0f ( n_cpcs_u_cog_l ) " & " %02.0f ( n_clust_cpcs_u_cog_l ) " \\ " _newline
file write `hh2' "    			& & & ( " %04.3f (cpcs_cogL_cpcsU_pv[1,1]) " ) & &   \\ " _newline
file write `hh2' "  & & CPCS Bottom 50\% & " %04.3f (cpcs_cogL_cpcsL_mean[1,1]) `star_cpcs_cogL_cpcsL' " & " %02.0f ( n_cpcs_l_cog_l ) " & " %02.0f ( n_clust_cpcs_l_cog_l ) " \\ " _newline
file write `hh2' "    			&	& & ( " %04.3f (cpcs_cogL_cpcsL_pv[1,1]) " ) & &   \\ " _newline

file write `hh2' "\\\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Dependent variables are standardized using the average and variance of the whole follow-up sample in the February 2022 survey. " _newline
file write `hh2' "\item (b) For the propensity score matching, we use covariates including student's grade, sex, baseline cognitive and baseline non-cognitive score, DT baseline time, branch dummy (location), parents' income source, last income per family member, number of household members, age of household head, education level of household head, teacher's age, sex, and phone survey dummy." _newline
file write `hh2' "\item (c) Cutoffs are created based on whether their ability to perform each item at the baseline was higher or lower than the median. " _newline
file write `hh2' "\item (d) Wild cluster bootstrap p-values are reported within parentheses. Clusters are schools at the baseline." _newline
file write `hh2' "\item (e) $^*$ Significant at 10\% level; $^{**}$ significant at 5\% level; $^{***}$ significant at 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
